import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * author 大蒋
 * git仓库代码迁移新仓库
 */
public class AppRun {

//    旧仓库主分支
    private static final String originHouse="master";
//    新仓库的唯一分支（主分支）
    private static final String newHouse="main";

    /**
     * 旧仓库的主分支会直接覆盖新仓库的主分支
     * 一般主分支不会频繁变化所以启动代码前直接修改上面两个仓库分支常量
     * @param args
     */
    public static void main(String[] args)  {
        Scanner scanner = new Scanner(System.in);
        StringBuilder stringBuilder = new StringBuilder();
        System.out.println("初始化。。。。。。"+"\n"+"请输入进行操作的文件夹地址");
        String url=scanner.nextLine();

        while (true){
            System.out.println("输入旧仓库的url");
            String origin=scanner.nextLine();
            System.out.println("输入新仓库的url");
            String update=scanner.nextLine();
            String[] split = origin.split("/");
            String originName = split[split.length - 1].split("\\.")[0];
            stringBuilder.append("cmd /c cd \""+url+"\"").append(" && ");
            stringBuilder.append("git clone"+" "+origin);
            outCommand(stringBuilder.toString());
            stringBuilder.setLength(0);

            String projectUrl = "cmd /c cd \""+url+"\\"+originName+"\"";

            stringBuilder.append(projectUrl).append(" && ");
            stringBuilder.append( "git remote add n1 "+update).append(" && ");
            stringBuilder.append("git pull n1 "+newHouse);
            outCommand(stringBuilder.toString());
            stringBuilder.setLength(0);

            stringBuilder.append(projectUrl).append(" && ");
            stringBuilder.append("git checkout "+newHouse).append(" && ");
            stringBuilder.append("git merge origin/"+originHouse+" --strategy=recursive --strategy-option=theirs --allow-unrelated-histories --no-edit").append(" && ");
            stringBuilder.append("git push n1 "+newHouse);
            outCommand(stringBuilder.toString());
            stringBuilder.setLength(0);

            outCommand(projectUrl+" && "+"git branch -a");
            branchMethod(scanner, stringBuilder, url, projectUrl);

            outCommand(projectUrl+" && rmdir /s /q "+originName);
        }
    }

    /**
     * 分支方法运行
     * */
    public static void branchMethod(Scanner scanner, StringBuilder stringBuilder, String url, String projectUrl) {
        while (true){
            System.out.println("输入你要选择的操作0 跳过重新输入 1 进行下一步 2准备下一个工程");
            int i = scanner.nextInt();
            scanner.nextLine();
            switch (i){
                case 0:
                    continue;
                case 1:
                    System.out.println("输入你要push的分支");
                    String branch = scanner.nextLine();
                    stringBuilder.append(projectUrl).append(" && ");
                    stringBuilder.append("git checkout "+branch).append(" && ");
                    stringBuilder.append("git push n1 "+branch).append(" && ");
                    stringBuilder.append("git branch -a");
                    outCommand(stringBuilder.toString());
                    stringBuilder.setLength(0);
                    break;
                case 2:
                    outCommand("cmd /c  cd \""+url+"\"");
                    return;
            }
        }
    }

    /**
     * 外部命令运行
     * */
    public static void outCommand(String command) {
        try {
            System.out.println(command+"\n");
            Process process = Runtime.getRuntime().exec(command);
            InputStream inputStream = process.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);  // 打印命令的输出
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
